/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * conectarInternalFrame.java
 *
 * Created on 6/10/2011, 10:00:07 AM
 */

package proyectosudp;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import proyectosudp.src.ClienteUDP;

/**
 *
 * @author ANALISTA
 */
public class conectarInternalFrame extends javax.swing.JInternalFrame {

    char rol; //variable en la que se almacena el rol del usuario
    ClienteUDP cliente = null;
    /** Creates new form conectarInternalFrame */
    public conectarInternalFrame(ClienteUDP cliente) {
        initComponents();
        jTextField1.setText("71228348");
        this.cliente = cliente;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("proyectosudp/resources/conectarInternalFrame"); // NOI18N
        setTitle(bundle.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(null);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(conectarInternalFrame.class);
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(160, 150, 100, 23);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(30, 70, 340, 14);

        jTextField1.setName("jTextField1"); // NOI18N
        getContentPane().add(jTextField1);
        jTextField1.setBounds(130, 110, 160, 20);

        jLabel2.setName("jLabel2"); // NOI18N
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 210, 310, 30);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Evento del boton para conectarse
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(jTextField1.getText()!=null && !jTextField1.getText().equals("")){
            try {
                try {
                    rol = cliente.consultarPrivilegios(jTextField1.getText()).toCharArray()[0];
                    if(rol=='C'||rol=='E'){
                        //TODO: habilitar los menú
                        if(rol=='C'){
                            JOptionPane.showMessageDialog(this, "Conectado correctamente, usted tiene privilegios de consulta");
                        }else{
                            JOptionPane.showMessageDialog(this, "Conectado correctamente, usted tiene privilegios de edición");
                        }
                        this.dispose();
                    }else{
                        jLabel2.setVisible(true);
                        JOptionPane.showMessageDialog(this, "Este usuario no tiene privilegios en el sistema, por favor ingrese un usuario válido");
                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(conectarInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (Exception ex) {
                Logger.getLogger(conectarInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }else{
            jLabel2.setText("Por favor ingrese un usuario válido");
        }
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
